package com.cn.boot.student.service.rbac.impl;

import com.cn.boot.student.entity.PermissionEntity;
import com.cn.boot.student.entity.UserEntity;
import com.cn.boot.student.service.rbac.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;

@Service("userDetailsService")
public class MyUserDetailsServiceImpl implements UserDetailsService {
    @Autowired
    private IUserService iUserService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserEntity user= iUserService.getUserByName(username);
        List<PermissionEntity> permissionList= iUserService.getPermissionsByUserId(user.getId());
        HashSet<PermissionEntity> permissions = new HashSet<>(permissionList);
        user.setAuthorities(permissions);
        return user;

    }
}
